package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.model.entity.other.ScanProcess;
import eteam.aps.model.entity.other.ScanReport;
import eteam.aps.model.vo.scan.ScanInvVo;
import eteam.aps.model.vo.scan.ScanProcessShow;
import eteam.aps.service.MocoRdersProcessService;
import eteam.aps.zn.ZNApiConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author LiuLei
 */
@Api(tags = "工单工序")
@RestController
@RequestMapping("/mrpc")
public class MocoRdersProcessController {

    @Autowired
    private MocoRdersProcessService mocoRdersProcessService;

    @PostMapping("sinfo")
    @ApiOperation("扫描基本信息")
    @ApiImplicitParam(name = "ScanProcess", value = "扫描信息")
    public R<ScanProcessShow<?>> scanInfo(@RequestBody ScanProcess scanProcess) throws Exception {
        if (Helper.isEmpty(scanProcess.getScan()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描内容不能为空！！");
        return R.success(mocoRdersProcessService.scanInfo(scanProcess));
    }

    @PostMapping("submit")
    @ApiOperation("报工")
    @ApiImplicitParam(name = "ScanProcess", value = "扫描信息")
    public R<ScanProcessShow<Void>> submit(@RequestBody ScanProcess scanProcess) throws Exception {
        if (!ZNApiConstant.SUBMIT.contains(scanProcess.getScan()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "报工只能是00,01！！");
        if (Helper.isEmpty(scanProcess.getBarCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描条码不能为空！！");
        if (Helper.isEmpty(scanProcess.getWpCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描工序不能为空！！");
        if (Helper.isEmpty(scanProcess.getUserCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描内容不能为空！！");
        return R.success(mocoRdersProcessService.submit(scanProcess));
    }

    @PostMapping("sureSubmit")
    @ApiOperation("确认报工")
    @ApiImplicitParam(name = "ScanProcess", value = "扫描信息")
    public R<ScanProcessShow<Void>> sureSubmit(@RequestBody ScanProcess scanProcess) throws Exception {
        if (!ZNApiConstant.SUBMIT.contains(scanProcess.getScan()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "报工只能是00,01！！");
        if (Helper.isEmpty(scanProcess.getBarCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描条码不能为空！！");
        if (Helper.isEmpty(scanProcess.getWpCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描工序不能为空！！");
        if (Helper.isEmpty(scanProcess.getUserCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描内容不能为空！！");
        return R.success(mocoRdersProcessService.sureSubmit(scanProcess));
    }

    @PostMapping("spro")
    @ApiOperation("查询工艺物料维护信息")
    @ApiImplicitParam(name = "ScanProcess", value = "扫描信息")
    public R<ScanInvVo> selectInv(@RequestBody ScanProcess scanProcess) {
        // 判断物料编码/条码/工序/扫描人 都不为空 就可以进行物料扫描添加
        if (Helper.isEmpty(scanProcess.getBarCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描条码不能为空！！");
        if (Helper.isEmpty(scanProcess.getWpCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描工序不能为空！！");
        if (Helper.isEmpty(scanProcess.getUserCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描人员不能为空！！");
        return R.success(mocoRdersProcessService.selectInv(scanProcess));
    }

    @PostMapping("sinv")
    @ApiOperation("扫描物料信息")
    @ApiImplicitParam(name = "ScanProcess", value = "扫描信息")
    public R<ScanProcessShow<ScanInvVo>> scanInv(@RequestBody ScanProcess scanProcess) throws Exception {
        // 判断物料编码/条码/工序/扫描人 都不为空 就可以进行物料扫描添加
        if (Helper.isEmpty(scanProcess.getScan()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描物料不能为空！！");
        if (Helper.isEmpty(scanProcess.getBarCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描条码不能为空！！");
        if (Helper.isEmpty(scanProcess.getWpCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描工序不能为空！！");
        if (Helper.isEmpty(scanProcess.getUserCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描人员不能为空！！");
        if (Helper.isEmpty(scanProcess.getInvCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "物料编号不能为空！！");
        return R.success(mocoRdersProcessService.scanInv(scanProcess));
    }

    @PostMapping("dinv")
    @ApiOperation("撤销物料信息")
    @ApiImplicitParam(name = "ScanProcess", value = "扫描信息")
    public R<ScanProcessShow<ScanInvVo>> delInv(@RequestBody ScanProcess scanProcess) throws Exception {
        // 判断物料编码/条码/工序/扫描人 都不为空 就可以进行物料扫描添加
        if (Helper.isEmpty(scanProcess.getBarCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描条码不能为空！！");
        if (Helper.isEmpty(scanProcess.getWpCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描工序不能为空！！");
        if (Helper.isEmpty(scanProcess.getInvFid()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "fid不能为空！");
        return R.success(mocoRdersProcessService.delInv(scanProcess));
    }

    @PostMapping("stotal")
    @ApiOperation("查询当日报工情况")
    @ApiImplicitParam(name = "ScanProcess", value = "扫描信息")
    public R<ScanReport> selectTotal(@RequestBody ScanProcess scanProcess) {
        if (Helper.isEmpty(scanProcess.getWpCode()) && Helper.isEmpty(scanProcess.getUserCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "工序或操作员不能都为空！！");
        return R.success(mocoRdersProcessService.selectTotal(scanProcess));
    }

    @PostMapping("slt")
    @ApiOperation("查询最近三天记录")
    @ApiImplicitParam(name = "ScanProcess", value = "扫描信息")
    public R<Map<String, Object>> selectLastThree(@RequestBody ScanProcess scanProcess) {
        if (Helper.isEmpty(scanProcess.getWpCode()) && Helper.isEmpty(scanProcess.getUserCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "工序或操作员不能都为空！！");
        return R.success(mocoRdersProcessService.selectLastThree(scanProcess));
    }

}
